package com.vincent.baseapp.base;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.Toast;

import com.vincent.baseapp.R;
import com.vincent.baseapp.util.CommonUtil;
import com.vincent.baseapp.util.VLog;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/6/2 0002.
 */
public abstract class BaseActivity<V, T extends BasePresenter<V>> extends AppCompatActivity implements IBaseView {
    public T presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.fragment_right_in, R.anim.fragment_left_out);
        setContentView(getLayoutResId());
        presenter = initPresenter();
        presenter.attach((V) this);
        ButterKnife.bind(this);
        initViewData();
    }

    /**
     * get layoutResId
     *
     * @return
     */
    public abstract int getLayoutResId();

    /***
     * init view data
     */
    public abstract void initViewData();

    /**
     * init presenter
     *
     * @return
     */
    public abstract T initPresenter();

    @Override
    protected void onDestroy() {
        presenter.detach();
        ButterKnife.unbind(this);
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            actFinish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * finish activity
     */
    public void actFinish() {
        this.finish();
        overridePendingTransition(R.anim.fragment_left_in, R.anim.fragment_right_out);
    }

    public void Lg(String message) {
        VLog.d(getClass().getSimpleName(), message);
    }

    @Override
    public void showLoadDialog() {
        CommonUtil.showLoadingView(this);
    }

    @Override
    public void dismissLoadDialog() {
        CommonUtil.dismissLoadingView(this);
    }

    @Override
    public void showToast(String content) {
        Toast.makeText(this, content, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}
